package com.yehun.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yehun.dao.RoleDao;
import com.yehun.dao.UserDao;
import com.yehun.entity.PagerEntity;
import com.yehun.entity.Role;
import com.yehun.entity.User;
import com.yehun.exception.ErrorException;
import com.yehun.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Resource
    private RoleDao roleDao;

    @Override
    public List<User> list() {
        return userDao.list();
    }

    @Override
    public PagerEntity<User> listPager(int pageIndex, int pageSize) {
        PagerEntity<User> pager = new PagerEntity<>();
        Page page = PageHelper.startPage(pageIndex, pageSize);
        pager.setData(userDao.list());
        pager.setTotal(page.getTotal());
        pager.setPageIndex(pageIndex);
        pager.setPageSize(pageSize);
        return pager;
    }

    @Override
    public User get(int id) {
        return userDao.get(id);
    }

    @Override
    public User getByUserId(String userId) {
        return userDao.getByUserId(userId);
    }

    @Override
    public User getByEmail(String email) {
        return userDao.getByEmail(email);
    }

    @Override
    public int save(User user) throws ErrorException {
        User userTmp = userDao.getByEmail(user.getEmail());
        if (userTmp != null) {
            throw new ErrorException("此邮箱已经使用");
        }
        int row =0;
        row += roleDao.updateUserNumber(user.getRoleId());
        row += userDao.save(user);
        return row;
    }

    @Override
    @Transactional
    public int updatePermission(String userId, int roleId, Long permission) throws ErrorException {
        User user = userDao.getByUserId(userId);
        if (user == null) {
            throw new ErrorException("未找到用户");
        }
        Role role = roleDao.get(roleId);
        if (role == null) {
            throw new ErrorException("未找到角色");
        }
        int row = 0;
        row += roleDao.updateUserNumber(user.getRoleId());
        row += userDao.updateRole(user.getUserId(), role.getId(), role.getName());
        row += userDao.updatePermission(user.getUserId(), permission);
        return row;
    }

    @Override
    public int remove(Integer[] idArray) throws ErrorException {
        if (idArray == null || idArray.length == 0) {
            throw new ErrorException("用户id数组为空");
        }
        int row = 0;
        List<Integer> roleIdArray = new LinkedList<>();
        User userTmp;
        for (Integer id : idArray) {
            userTmp = userDao.get(id);
            if (userTmp == null) {
                throw new ErrorException(String.format("用户id[%d]未找到用户", id));
            }
            roleIdArray.add(userTmp.getRoleId());
            row += userDao.remove(id);
        }
        if (!roleIdArray.isEmpty()) {
            for (Integer roleId : roleIdArray) {
                row += roleDao.updateUserNumber(roleId);
            }
        }
        return row;
    }
}
